var conditionSetting = {
	init : function() {
		$('.tab-alarm').height(nav.whiceH[1]);
		this.tabAlarmScroll();
		this.action="insert";
		this.delId="";
		this.validata=true;
		this.msg="";
		this.delAnalyseGroupsId="";
		this.queryTabInit = this.quaryTab();//this.queryTabInit.reflash(0);
		this.choseCar();
		this.annal();
		this.allVehicleId="";
		this.allVehicleHtml="";//用来储存用户拥有的车辆
		this.initEvent();
		conditionSetting.alarmId = '';
		//this.loadUserVehicle();

	},my97DateInte:function(){
		$("#inputStartDate").on("click",function(){
			WdatePicker({
				maxDate:$("#inputEndDate").val()
			});
		});
		$("#inputEndDate").on("click",function(){
			WdatePicker({
				minDate:$("#inputStartDate").val()
			});
		});
	},loadUserVehicle :function(keyWord){
		//加载用户所有的车辆
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/loadUserVehicle.html",
			cache : false,
			data:{"analyseID":"1","keyWord":keyWord},
			dataType : "JSON",
			success : function(data) {
				if(data.vehicleList && data.vehicleList.length>0){
					var arr = data.vehicleList;
					var size = arr.length;
					var  html="";
					var  selectStr="";
					conditionSetting.allVehicleId="";
					for (var i = 0; i < size; i++) {
						if (i == 0) {
							conditionSetting.allVehicleId+=arr[i].vehicleId+","
							html += '<li class="'
								+ 'list active"><a class=' + '"db'
								+ '" href=' + '"javascript:;' + '">'
								+ arr[i].registrationNO
								+ '</a> <div style="'
								+ 'display: none"' + '>'
								+ arr[i].vehicleId + ',</div></li>'
						} else {
							conditionSetting.allVehicleId+=arr[i].vehicleId+","
							html+='<li class="'
								+ 'list"><a class=' + '"db'
								+ '" href=' + '"javascript:;' + '">'
								+ arr[i].registrationNO
								+ '</a> <div style="'
								+ 'display: none"' + '>'
								+ arr[i].vehicleId + ',</div></li>'
						}
					}
					conditionSetting.allVehicleHtml=html;
					$(".queryCarCont").html(html);
				}/*else{
					$("#content").text("无可设置报警条件的车辆!");
					$("#velidateModal").modal("show");
				}*/
				
			}
		});
		
	},
	initEvent : function() {
		$(function(){
			WdatePicker({
				autoShowQS:false,
				isShowToday:false
			});
			
		})

        $("#allVehicle").on("click",function(){
      	  var obj = document.getElementById("vehicleTab");
            obj.style.display= "none";
        })
        $("#chooseVehicle").on("click",function(){
      	  var obj = document.getElementById("vehicleTab");
      	  obj.style.display= "block";
        })
		$("#vehicleSpeed").on("click",function(){
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/vehicleSpeed/vehicleSpeedIndex.html",
				cache : false,
				dataType : "JSON",
				success : function(data) {
				
				}
			});
		});
		
		
		
		
		$("#comDelBtn").click(function(){
			conditionSetting.deleteVehicleSpeedComf();
		})
		
		//点击搜索车辆
		$("#vehicleSearch").click(function() {//
		
		 var value = $("#queryValue").val();
		 conditionSetting.loadUserVehicle(value);			
						})
		 $("#saveOrUpdateTab").on("click",function(){
			 conditionSetting.action="insert";
			 $("#queryValue").val("");
			 $(".queryCarCont").html("");
	     conditionSetting.alarmId = "";
	     conditionSetting.loadUserVehicle();
		 //$(".queryCarCont").html(conditionSetting.allVehicleHtml);
	     
	     
		 $("#remindInterval").val("");
		 $("#earlyWarningThreshold").val("");
		 $("#name").val("");
		 $("#analysegroupId ").val("");
		 $("#speedThreshold").val("");
		 $("#remark").val("");
		 $("#timeThreshold").val("");
		 $(".resultCarCont li").remove();
		 $("#commandContent").val("");
		 $("#wechart").attr("checked",false);
		 $("#messager_notify").attr("checked",false)
		 $("#remindType2").attr("checked",true);
		 $("#analyseTypeHeight").attr("checked",true);
		 $("#allVehicle").attr("checked","checked");
		 $("#inputStartDate").val("00:00:00");
			$("#inputEndDate").val("23:59:59");
        	  var obj = document.getElementById("vehicleTab");
              obj.style.display= "none";
              
          
       });
		$("#saveVehicleSpeed").click(function() {
			conditionSetting.saveVehicleSpeed();
		});
		$(".queryCarCont ").on("click", "li", function() {
			var _this = this;
			$(_this).toggleClass("active");
		});
		$(".queryCarCont ").on("dblclick", "li", function() {
			var _this = this;
			$(this).remove();
			//$(this).addClass("active");
			$(".resultCarCont").prepend(this);

		})
		$(".resultCarCont ").on("dblclick", "li", function() {
			var _this = this
			$(_this).remove();
			$(".queryCarCont").prepend(_this);

		})

		$(".resultCarCont").on("click", "li", function() {
			$(this).toggleClass("active");
		});
		$(".resultCarCont ").on("dblclick", "li", function() {
			var _this = this
			$(_this).remove();
			$(".queryCarCont").prepend(_this);

		});
		$(".go").click(function() {
			$(".resultCarCont").append($(".queryCarCont .active"));
			$(".queryCarCont .active").remove();
		});
		$(".back").click(function() {
			$(".queryCarCont").prepend($(".resultCarCont .active"));
			$(".resultCarCont .active").remove();
		});


		 $("#validateBtn").click(function(){
			$("velidateModal").modal("hide");
		})
		 $('#velidateModal').on('hidden', function() {
		$("#content p").remove();
				})
	},

	quaryTab : function() {
		var index = 0;
		var _this = this;
		var func = function(){
			$('.tab-alarm-cont-list').hide().eq(index).show();
			$(this).addClass('active');
			$(this).siblings().removeClass('active');
			$('.date-alarm').find('.line').css({
				'left' : 100 * index
			});
		};
		$('.date-alarm').find('.list').click(function() {
			index = $(this).index();
			func();
		});
		return {
			'index' : index,
			reflash : function(i){
				index = i;
				func();
			}
		}
	},

	/**
	 * 车辆选择
	 */
	choseCar : function() {
		$('#selectCar').select2();
	},
	updateVehicleSpeed : function(id,analyseGroupsID) {
		conditionSetting.action="update";
		//global.ajaxLoading.pop("",true,2000);
		 conditionSetting.alarmId = id;
		 $(".queryCarCont").html("");
		 conditionSetting.loadUserVehicle("");
		$("#chooseVehicle").attr("checked","checked");
		 var obj = document.getElementById("vehicleTab");
     	  obj.style.display= "block";
     	  
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + 'reportset/vehicleSpeed/queryVehicle.html',
			cache : false,
			data : {"id":analyseGroupsID},
			dataType : "JSON",
			success : function(data) {
				var arr=data.rows;
				var size=arr.length;
				var vehicleArr=conditionSetting.vehicleArr
				var html="";
				for (var i = 0; i < size; i++) {
						html+='<li class="' + 'list "><a class=' + '"db'
										+ '" href=' + '"javascript:;' + '">'
										+ arr[i].registrationNo
										+ '</a> <div style="'
										+ 'display: none"' + '>'
										+ arr[i].vehicleId + ',</div></li>';
				}
				$(".resultCarCont").html(html);
				
			}
		});
		 $.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/updateVehicleSpeed.html",
			cache : false,
			data : {"id":id},
			dataType : "JSON",
			success : function(data) {
				var data=data.data;
				$("#name").val(data.name);
				$("#speedThreshold").val(data.speedThreshold);
				$("#remark").val(data.remark);
				$("#analysegroupId ").val(data.analyseGroupsID);
				
				var timeThreshold=(data.timeThreshold)/60;
				   var tnum=timeThreshold.toString().indexOf(".")
					if(tnum>0){
						timeThreshold=timeThreshold.toFixed(2);
				}
				
				$("#timeThreshold").val(timeThreshold);
				$("#commandContent").val(data.commandContent);
				 $("#inputStartDate").val(data.startTime);
					$("#inputEndDate").val(data.endTime);
					
					
					
					var remindInterval=(data.remindInterval)/60;
					
					   var rnum=remindInterval.toString().indexOf(".")
						if(rnum>0){
							remindInterval=remindInterval.toFixed(2);
					}
					
					$("#remindInterval").val(remindInterval);
					$("#earlyWarningThreshold").val(data.earlyWarningThreshold);
					
				if(data.analyseType==1){
					$("#analyseTypeHeight").attr("checked","checked");
				}else{
					$("#analyseTypeLow").attr("checked","checked");
				}
				if(data.remindType==2){
					$("#remindType2").attr("checked","checked");
				}else if(data.remindType==3){
					$("#remindType3").attr("checked","checked");
				}else{
					$("#remindType4").attr("checked","checked");
				}
				if(data.is_weixin_notify==1){
					$("#wechart").attr("checked","checked")
					}else{
						 $("#wechart").attr("checked",false);
					};
				if(data.is_sms_notify==1){
					$("#messager_notify").attr("checked","checked")
				}else{
					$("#messager_notify").attr("checked",false);
				}
			}
		});
		 conditionSetting.queryTabInit.reflash(1);
	},
	saveVehicleSpeed : function() {
		
		
		var param=conditionSetting.getParams();
		if(!conditionSetting.validata){
			global.ajaxLoading.pop(conditionSetting.msg,false,2000);
			return;
		}
	
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "reportset/vehicleSpeed/saveVehicleSpeed.html",
				cache : false,
				data : param,
				dataType : "JSON",
				success : function(data) {
	 
			  $("#annal").flexReload();
               global.ajaxLoading.pop(data.msg,false,1000);
               
				}

			});
		
		conditionSetting.queryTabInit.reflash(0);

	},deleteVehicleSpeedComf :function(){
		
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/deleteVehicleSpeed.html",
			cache : false,
			data : {
				"id" : conditionSetting.delId,
				"analyseGroupsId":conditionSetting.delAnalyseGroupsId
			},
			dataType : "JSON",
			success : function(data) {
			}

		});
		$.ajax({
			type : 'post',
			async : true,
			url : appCtx + "reportset/vehicleSpeed/deleteVehicleSpeedVehicle.html",
			cache : false,
			data : {
				
				"analyseGroupsId":conditionSetting.delAnalyseGroupsId
			},
			dataType : "JSON",
			success : function(data) {
			}

		});
		$("#annal").flexReload();
		$("#myModalDel").modal("hide");
	},
	/**
	 * 删除超速设置
	 */

	deleteVehicleSpeed : function(id,analyseGroupsId) {
		conditionSetting.delId=id;
		conditionSetting.delAnalyseGroupsId=analyseGroupsId;
		

	},
/**
 * 查看监控车辆
 * @param id
 */	
queryVehicle : function(id) {

		$('#myModal').on('hidden', function() {
			$(".ownCarCont li").remove();
		})
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "reportset/vehicleSpeed/queryVehicle.html",
			cache : false,
			data : {
				"id" : id
			},
			dataType : "JSON",
			success : function(data) {
				var vehicleArr=data.rows;
				var size=vehicleArr.length;

                 var html= '<li class="list "><a class="db" href="javascript:;"><span style="color: blue;">车牌号:</span></a></li> '; //
				for (var i = 0; i < size; i++) {//
					html+='<li class="' + 'list "><a class=' + '"db'//
										+ '" href=' + '"javascript:;' + '">'//
										+ vehicleArr[i].registrationNo//
										+ '</a> <div style="'//
										+ 'display: none"' + '>'//
										+ vehicleArr[i].vehicleId + ',</div></li>';//
				}
				$(".ownCarCont").html(html);
			}

		});
		
		
	},
	annal : function() {
		/**
		 * 操作列
		 */
		function formmatOperation(id,analyseGroupsID) {
			var handleStr = "";

			handleStr = '<a href="javascript:void(0)" onclick="conditionSetting.updateVehicleSpeed('//
				+ id+','+analyseGroupsID//
				+ ')" title=""'+'" >  <span style='+'"color: blue;'+'">修改</span></a>'+'&nbsp;&nbsp;<a href="#myModalDel" onclick="conditionSetting.deleteVehicleSpeed('//
				+ id +','+analyseGroupsID//
				+ ')" '+'data-toggle="'+'modal"'+'title="'+'" >  <span style='+'"color: blue;'+'">删除</span></a>';//

			return handleStr;
		}
		;
		function formmatVehicle(analyseGroupsID) {
			var handleStr = "";
			handleStr = '<a href="' + '#myModal' + '"  role=' + '"button'
					+ '" onclick=' + '"conditionSetting.queryVehicle('
					+ analyseGroupsID + ')" data-toggle=' + '"modal'
					+ '"><span style=' + '"color: blue;' + '"</span>查看车辆</a>';
			return handleStr;
		};

		$("#annal").flexigrid({
			dataType : 'json',
			url : appCtx + 'reportset/vehicleSpeed/queryAllVehicleSpeed.html',
			params : [],
			contentType : "application/json; charset=utf-8",
			method : 'POST',
			widthType:'%',
	        width:$('.tab-alarm-cont').width(),
			colModel : [ {
				display : '条件名称',
				name : 'name',
				width : 5,
				sortable : false,
				// hide:true,
				align : 'left',
				checked : 'checked',
				paramcolnames : [ 'name'],
				handlefunction : function(name) {
						return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+name+'" >'+name+'</a>';
					}
			}, {
				display : '速度阈值',
				name : 'speedThreshold',
				width : 5,
				sortable : false,
				align : 'left',
			},{
				display : '时间门限',
				name : 'timeThreshold',
				width : 5,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'timeThreshold' ],
				handlefunction : function(timeThreshold) {
					if(timeThreshold){
						   timeThreshold=timeThreshold/60;
						   var num=timeThreshold.toString().indexOf(".")
						if(num>0){
							timeThreshold=timeThreshold.toFixed(2);
					}
				}
					return timeThreshold;
				}
			},{
				display : '时间间隔',
				name : 'remindInterval',
				width : 5,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'remindInterval' ],
				handlefunction : function(remindInterval) {
					if(remindInterval){
						remindInterval=remindInterval/60;
						   var num=remindInterval.toString().indexOf(".")
						if(num>0){
							remindInterval=remindInterval.toFixed(2);
					}
				}
					return remindInterval;
				}
			},{
				display : '预报警阈值',
				name : 'earlyWarningThreshold',
				width : 6,
				sortable : false,
				align : 'left',
			},/* {
				display : '周期',
				name : 'periodValue',
				width : 80,
				sortable : false,
				align : 'left',
			},*/ {
				display : '时间范围',
				name : 'startTime',
				width : 11,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'startTime', 'endTime' ],
				handlefunction : function(startTime, endTime) {
					return startTime + "至" + endTime;
				}
			}, {
				display : '分析类型',
				name : 'analyseType',
				width : 5,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'analyseType' ],
				handlefunction : function(analyseType) {
					if(analyseType==1){
						analyseType= "高速";
					}else{
						analyseType= "低速";
					}
					return  analyseType;
				}
			},{
				display : '提醒方式',
				name : 'remindType',
				width :10,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'remindType' ],
				handlefunction : function(remindType) {
					if(remindType==2){
						remindType="平台报警";
					}else if(remindType==3){
						remindType="TTS语音播报"
					}else{
						remindType="平台/TTS语音播报"
					}
					return remindType;
				}
			},{
				display : '微信通知',
				name : 'is_weixin_notify',
				width : 5,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'is_weixin_notify' ],
				handlefunction : function(is_weixin_notify) {
					if(is_weixin_notify==1){
						is_weixin_notify="是";
					}else{
						is_weixin_notify="否";
					}
					return is_weixin_notify;
				}
			},{
				display : '创建时间',
				name : 'createDatetime',
				width :12,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'createDatetime' ],
				handlefunction : function(createDatetime) {
					var size=createDatetime.split(".").length;
					if(size>1){
					createDatetime=createDatetime.split(".")[0];
					
					}
					return createDatetime;
				}
				
			},{
				display : '短信通知',
				name : 'is_sms_notify',
				width : 5,
				sortable : false,
				align : 'center',
				paramcolnames : [ 'is_sms_notify' ],
				handlefunction : function(is_sms_notify) {
					if(is_sms_notify==1){
						is_sms_notify="是";
					}else{
						is_sms_notify="否";
					}
					return is_sms_notify;
				}
			},{
				display : '监控车辆',
				name : 'Handler',
				width :6,
				sortable : false,
				align : 'center',

				paramcolnames : [ 'analyseGroupsID' ],
				handlefunction : formmatVehicle,
			}, {
				display : '播报内容',
				name : 'commandContent',
				width : 7,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'commandContent'],
				handlefunction : function(commandContent) {
						return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+commandContent+'" >'+commandContent+'</a>';
					}
			},{
				display : '备注',
				name : 'remark',
				width : 5,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'remark'],
				handlefunction : function(remark) {  
						return '<a class=\"textOverFlow\" href=\"javascript:;\" title=\"'+remark+'" >'+remark+'</a>';
					}
				
			}, {
				display : '操作',
				name : 'operation',
				width : 8,
				sortable : false,
				align : 'left',
				paramcolnames : [ 'iD','analyseGroupsID' ],
				handlefunction : formmatOperation
			} ],

			resizable : false,
			sortname : "terminalId",// 第一次加载数据时排序列
			sortorder : "desc",// 第一次加载数据时排序类型
			usepager : false,// 是否分页，默认为true。
			showToggleBtn : false,
			showTableToggleBtn : false,// 是否显示收起/打开按钮,默认不显示。
			// useRp : true,// 是否可以动态设置每页显示的结果数，默认为false。
			// rp : 10,// 每页记录数，默认为10
			// rpOptions: [10,50,100,300,500], // 每页结果数
			checkbox : true,// 是否要多选框,默认为false。
			rowId : 'terminalId',// 多选框绑定行的id,只有checkbox :
			// true时才有效。
			singleSelect : true,
			height : nav.whiceH[1] - 130, // 表格高度
			titDisplay : false,
			onSuccess : function(data) {
			},
			onSelectRow : function(row) {

			},
			onSelectAllRow : function(data) {

			}

		});
	},
	
	tabAlarmScroll : function(){
		$(document).scroll(function(e) {
			var scrollTop = $(document).scrollTop();
			if (scrollTop > 0) {
				if (scrollTop < 42) {
					$('.tab-alarm').css({
						'position' : 'fixed',
						'height' : nav.whiceH[0] + 'px',
						'top' : 42 - scrollTop + 'px',
						'left' : '41px',
						'z-index' : 1000
					});
				} else {
					$('.tab-alarm').css({
						'position' : 'fixed',
						'height' : nav.whiceH[0] + 'px',
						'top' : 0,
						'left' : '41px',
						'z-index' : 1000
					});
				}
			} else {
				$('.tab-alarm').css({
					'position' : 'absolute',
					'height' : nav.whiceH[1] + 'px',
					'top' : 0,
					'left' : '1px'
				});
			}
		});
	},getParams:function(){
		conditionSetting.validata=true;
		conditionSetting.msg="";
		var action=conditionSetting.action;
		var ids = $(".resultCarCont li div").text();
		if(!ids){
			ids=conditionSetting.allVehicleId;
		}
		if($("#allVehicle").attr("checked")=="checked"){
			ids=conditionSetting.allVehicleId;
		}
		var name = $.trim($("#name").val());
		var remindInterval = $.trim($("#remindInterval").val());
		var earlyWarningThreshold = $.trim($("#earlyWarningThreshold").val());
		if(remindInterval){
			remindInterval=(remindInterval*60).toFixed(0);
		}
		if(!remindInterval){
			remindInterval=600;
		}
		
		
		var speedThreshold = $.trim($("#speedThreshold").val());
		
		

		var remark = $.trim($("#remark").val());
		var analysegroupId = $("#analysegroupId").val();
		var start = $("#inputStartDate").val();
		var end = $("#inputEndDate").val();
		var startTime=start;
		var endTime=end;
		if(!startTime){
			startTime="00:00:00";
		}
		if(!endTime){
			endTime="23:59:59";
		}
		if(end<start){
			startTime=end;
			endTime=start;
		}
		if(!name){
			
			/*$("#content").append("  <p>请填写条件名称</p> ")
			$("#velidateModal").modal("show");
			return;*/
			conditionSetting.msg="【请填写条件名称】";
			conditionSetting.validata=false;
		}
		if(!speedThreshold){
			conditionSetting.msg="【请填超速阈值】";
			conditionSetting.validata=false;
			/*$("#content").append("  <p>请填超速阈值数字</p> ")
			$("#velidateModal").modal("show");
			return;*/
		}
		if(speedThreshold&&(speedThreshold>120||speedThreshold<0 )){
			conditionSetting.msg="【请填入0-120数字】";
			conditionSetting.validata=false;
			/*$("#content").append("  <p>请填入0-120数字</p> ")
			$("#velidateModal").modal("show");
			return;*/
		}
		if(!earlyWarningThreshold){
			if(speedThreshold>10){
				earlyWarningThreshold=speedThreshold-10;
			}else{
				earlyWarningThreshold=speedThreshold;
			}
		}
            if((earlyWarningThreshold-speedThreshold)>0){
            	conditionSetting.msg="【预报警阈值不能大于速度阈值】";
    			conditionSetting.validata=false;
            	/*$("#content").append("  <p>预报警阈值不能大于速度门限</p> ")
				$("#velidateModal").modal("show");
				return;*/
            }
		var timeThreshold=$.trim($("#timeThreshold").val());
		if(timeThreshold){
			timeThreshold=(timeThreshold*60).toFixed(0);
		}
		if(!timeThreshold){
			timeThreshold=600;
		}
		var analyseType;
		if($("#analyseTypeHeight").attr("checked")=="checked"){
			 analyseType=1;
		}else{
			 analyseType=2;
		}
		var commandContent=$.trim($("#commandContent").val());
		if(!commandContent ){
			commandContent="您已超速，请减速行驶!";
		}
		var remindType=2;
		if($("#remindType3").attr("checked")=="checked"){
			remindType=3;
		}
		if($("#remindType4").attr("checked")=="checked"){
			remindType=4;
		}
		var wechart=0;
		if($("#wechart").attr("checked")=="checked"){
			 wechart=1
		}
		var messager_notify=0;
		if($("#messager_notify").attr("checked")=="checked"){
			messager_notify=1
		}
			
		
		var param =  {
				
			"Name" : name,
			"SpeedThreshold" : speedThreshold,
			"PeriodValue" : "每日",
			"Remark" : remark,
			"ids" : ids,
			"action" : action,
			"AnalyseGroupsID":analysegroupId,
			"TimeThreshold":timeThreshold,
			"CommandContent":commandContent,
			"AnalyseType":analyseType,
			"RemindType":remindType,
			"Is_weixin_notify":wechart,
			"Is_sms_notify":messager_notify,
			"StartTime":startTime,
			"ID":conditionSetting.alarmId,
			"EndTime":endTime,
			"RemindInterval":remindInterval,
			"EarlyWarningThreshold":earlyWarningThreshold
		} 
		
		return param;
	}
};

conditionSetting.init();